<?php

class register_study extends Controller {

	function register_study()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->model('filemanagementmodel');
	    $data['banner'] = $this->filemanagementmodel->getByPostion('receipt');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		$data['form_status'] = 'add';
		$this->load->view('f_header', $data);
		$this->load->view('f_register_study', $data);
		$this->load->view('f_footer', $data);
	}
	
	function save()
	{
		$this->load->model('StudentModel');
		$this->load->model('StudentSchoolModel');
		$data = array();
		$title = $this->input->post('radTitle');
		$titleOther = $this->input->post('txtTitleOther');
		if($title != 'อื่นๆ') {
			$data['title'] = $title;
		} else {
			$data['title'] = $titleOther;
		}
		$data['first_name'] = $this->input->post('txtFirstName');
		$data['last_name'] = $this->input->post('txtLastName');
		$data['nick_name'] = $this->input->post('txtNickName');
		$currentClass = $this->input->post('txtCurrentClass');
		$currentClassOther = $this->input->post('txtCurrentClassOther');
		if($currentClass != 'สูงกว่า ป. ตรี (โปรดระบุ)') {
			$data['current_class'] = $currentClass;
		} else {
			$data['current_class'] = $currentClassOther;
		}	
		$data['birthday'] = $this->input->post('txtBirthday') ?  $this->input->post('txtBirthday') : null;
		$data['school'] = $this->input->post('txtSchool');
		$data['plan_edu'] = $this->input->post('txtPlan') ? $this->input->post('txtPlan') : null;
		$currentClass = $this->input->post('txtCurrentClass');
		$currentClassOther = $this->input->post('txtCurrentClassOther');
		if($currentClass != 'สูงกว่า ป. ตรี (โปรดระบุ)') {
			$data['current_class'] = $currentClass;
		} else {
			$data['current_class'] = $currentClassOther;
		}	
		$data['citizen_id'] = $this->input->post('txtCitizenId') ? $this->input->post('txtCitizenId') : null;
		$course = $this->input->post('txtCourse');
		$courseOther = $this->input->post('txtCourseOther');
		if($course != 'อื่นๆ (โปรดระบุ)') {
			$data['course_type'] = $course;
		} else {
			$data['course_type']  = $courseOther;
		}
		$data['source'] = 'C';
		
		$data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;    
	    $data['telephone_home'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
	    $data['mobile01'] = $this->input->post('txtMobile01') ? $this->input->post('txtMobile01') : null;
	    
		$netMobile = $this->input->post('selNetMobile01') ? $this->input->post('selNetMobile01') : null;
		$netMobileOther = $this->input->post('txtNetMobile01') ? $this->input->post('txtNetMobile01') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile01_network'] = $netMobile;
		} else {
			$data['mobile01_network'] = $netMobileOther;
		}
	    
		$data['mobile02'] = $this->input->post('txtMobile02') ? $this->input->post('txtMobile02') : null;
		$netMobile = $this->input->post('selNetMobile02') ? $this->input->post('selNetMobile02') : null;
		$netMobileOther = $this->input->post('txtNetMobile02') ? $this->input->post('txtNetMobile02') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['mobile02_network'] = $netMobile;
		} else {
			$data['mobile02_network'] = $netMobileOther;
		}
		
	    $data['add_room_no'] = $this->input->post('txtRoomNo') ? $this->input->post('txtRoomNo') : null;
	    $data['add_home_no'] = $this->input->post('txtHomeNo') ? $this->input->post('txtHomeNo') : null;
	    $data['add_building_name'] = $this->input->post('txtBuilding') ? $this->input->post('txtBuilding') : null;
	    $data['add_moo'] = $this->input->post('txtMoo') ? $this->input->post('txtMoo') : null;
	    $data['add_soi'] = $this->input->post('txtSoi') ? $this->input->post('txtSoi') : null;
	    $data['add_road'] = $this->input->post('txtRoad') ? $this->input->post('txtRoad') : null;
	    $data['add_district'] = $this->input->post('txtDistinct') ? $this->input->post('txtDistinct') : null;
	    $data['add_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
	    $data['add_province'] = $this->input->post('txtProvince') ? $this->input->post('txtProvince') : null;
	    $data['add_post_code'] = $this->input->post('txtPostCode') ? $this->input->post('txtPostCode') : null;
	    $data['add_note'] = $this->input->post('txtAddNote') ? $this->input->post('txtAddNote') : null;
	    
	    $data['first_name_writer'] = $this->input->post('txtWFName') ? $this->input->post('txtWFName') : null;
	    $data['last_name_writer'] = $this->input->post('txtWLName') ? $this->input->post('txtWLName') : null;
		$data['telephone_writer'] = $this->input->post('txtWMobile') ? $this->input->post('txtWMobile') : null;
		$netMobile = $this->input->post('selNetMobile03') ? $this->input->post('selNetMobile03') : null;
		$netMobileOther = $this->input->post('txtNetMobile03') ? $this->input->post('txtNetMobile03') : null;
		if($netMobile != 'อื่นๆ (โปรดระบุ)') {
			$data['telephone_network'] = $netMobile;
		} else {
			$data['telephone_network'] = $netMobileOther;
		}
	    
		$id = $this->StudentModel->addFromC($data, null);	
		
		$scArray = array('student_id' => $id, 'shool_id' => '1');
		$this->StudentSchoolModel->add($scArray);
		$d['id'] = $id;
		if(isset($_FILES) && @$_FILES['txtMap']['error'] == '0') {
			$config['upload_path'] = './data-image/student/';
			$config['allowed_types'] = 'gif|jpg|png|pdf';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-addMap';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('txtMap'))
			{
				$this->msg = "ไม่สามารถ upload รูปภาพได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'student_id' => $id, 
	               'add_image' => $data['upload_data']['file_name']   
	            );
				$this->StudentModel->updateStudentAddImagePath($Imgdata);
				$this->msg = "บันทึกข้อมูลเรียบร้อย";
			}
		}
		
		$this->load->model('filemanagementmodel');
		$data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
		$data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		
		$this->load->view('f_header', $data);
		$this->load->view('f_register_study_2', $d);
		$this->load->view('f_footer');
	}
	
	function save2()
	{
	$id = $this->input->post('txtID');
		if($id != null) {
			$d['id'] = $id;
			$this->load->model('StudentModel');
		    $data = array();
			$data['student_id'] = $id;
			
			$sub = $this->input->post('selSubjet01') ? $this->input->post('selSubjet01') : null;
			$subOter = $this->input->post('txtSubjectOther01') ? $this->input->post('txtSubjectOther01') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['booking_subject01'] = $sub;
			} else {
				$data['booking_subject01'] = $subOter;
			}
			
			$sub = $this->input->post('selSubjet02') ? $this->input->post('selSubjet02') : null;
			$subOter = $this->input->post('txtSubjectOther02') ? $this->input->post('txtSubjectOther02') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['booking_subject02'] = $sub;
			} else {
				$data['booking_subject02'] = $subOter;
			}
	
			$sub = $this->input->post('selSubjet03') ? $this->input->post('selSubjet03') : null;
			$subOter = $this->input->post('txtSubjectOther03') ? $this->input->post('txtSubjectOther03') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['booking_subject03'] = $sub;
			} else {
				$data['booking_subject03'] = $subOter;
			}
			
			
			$sub = $this->input->post('selSubjet04') ? $this->input->post('selSubjet04') : null;
			$subOter = $this->input->post('txtSubjectOther04') ? $this->input->post('txtSubjectOther04') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['booking_subject04'] = $sub;
			} else {
				$data['booking_subject04'] = $subOter;
			}
			
			
			$sub = $this->input->post('selSubjet05') ? $this->input->post('selSubjet05') : null;
			$subOter = $this->input->post('txtSubjectOther05') ? $this->input->post('txtSubjectOther05') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['booking_subject05'] = $sub;
			} else {
				$data['booking_subject05'] = $subOter;
			}
			
			$sub = $this->input->post('selSubjet06') ? $this->input->post('selSubjet06') : null;
			$subOter = $this->input->post('txtSubjectOther06') ? $this->input->post('txtSubjectOther06') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['booking_subject06'] = $sub;
			} else {
				$data['booking_subject06'] = $subOter;
			}
			
			$sub = $this->input->post('selObj') ? $this->input->post('selObj') : null;
			$subOter = $this->input->post('txtObjOther') ? $this->input->post('txtObjOther') : null;
			if($sub != 'อื่นๆ (โปรดระบุ)') {
				$data['objective'] = $sub;
			} else {
				$data['objective'] = $subOter;
			}
			
			$data['study_add_room_no'] = $this->input->post('txtRoomNo') ? $this->input->post('txtRoomNo') : null;
			$data['study_add_home_no'] = $this->input->post('txtHomeNo') ? $this->input->post('txtHomeNo') : null;
			$data['study_add_buliding_name'] = $this->input->post('txtBuilding') ? $this->input->post('txtBuilding') : null;
			$data['study_add_moo'] = $this->input->post('txtMoo') ? $this->input->post('txtMoo') : null;
			$data['study_add_soi'] = $this->input->post('txtSoi') ? $this->input->post('txtSoi') : null;
			$data['study_add_road'] = $this->input->post('txtRoad') ? $this->input->post('txtRoad') : null;
			$data['study_add_distinct'] = $this->input->post('txtDistinct') ? $this->input->post('txtDistinct') : null;
			$data['study_add_area'] = $this->input->post('txtArea') ? $this->input->post('txtArea') : null;
			$data['study_add_province'] = $this->input->post('txtProvince') ? $this->input->post('txtProvince') : null;
			$data['study_add_post_code'] = $this->input->post('txtPostCode') ? $this->input->post('txtPostCode') : null;
			$data['study_add_note'] = $this->input->post('txtAddNote') ? $this->input->post('txtAddNote') : null;
			
			$this->StudentModel->updateStudentBooking2($data);
			
			if(isset($_FILES) && @$_FILES['txtMap']['error'] == '0') {
				$config['upload_path'] = './data-image/student/';
				$config['allowed_types'] = 'gif|jpg|png|pdf';
				$config['max_size']	= '2048';
				$config['overwrite'] = TRUE;
				$config['file_name'] = $id.'-stuMap';
				$this->load->library('upload', $config);
				if ( ! $this->upload->do_upload('txtMap'))
				{
					$this->msg = "ไม่สามารถ upload รูปภาพได้";			
				}
				else
				{
					$data = array('upload_data' => $this->upload->data());
					$Imgdata = array(
					   'student_id' => $id, 
		               'study_add_map' => $data['upload_data']['file_name']   
		            );
					$this->StudentModel->updateStudyPath($Imgdata);
					$this->msg = "บันทึกข้อมูลเรียบร้อย";
				}
			}
			
			$this->load->model('filemanagementmodel');
			$data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
			$data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
			$this->load->view('f_header', $data);
			$this->load->view('f_register_study_3', $d);
			$this->load->view('f_footer');
			
		} else {
			$this->index();
		}
	}
	function save3()
	{
		$id = $this->input->post('txtID');
		if($id != null) {
			$this->load->model('StudentModel');
		    $data = array();
			$data['student_id'] = $id;
			
			$data['study_time_mon'] = $this->input->post('txtMon') ? $this->input->post('txtMon') : null;
			$data['study_time_tue'] = $this->input->post('txtTue') ? $this->input->post('txtTue') : null;
			$data['study_time_wed'] = $this->input->post('txtWed') ? $this->input->post('txtWed') : null;
			$data['study_time_thr'] = $this->input->post('txtThu') ? $this->input->post('txtThu') : null;
			$data['study_time_fir'] = $this->input->post('txtFri') ? $this->input->post('txtFri') : null;
			$data['study_time_sat'] = $this->input->post('txtSat') ? $this->input->post('txtSat') : null;
			$data['study_time_sun'] = $this->input->post('txtSun') ? $this->input->post('txtSun') : null;
			$data['study_start_date'] = $this->input->post('txtDatePicker') ? $this->input->post('txtDatePicker') : null;
			$data['note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
			
			$sub = $this->input->post('selKnown') ? $this->input->post('selKnown') : null;
			$subOter = $this->input->post('txtKnownOther') ? $this->input->post('txtKnownOther') : null;
			if($sub != 'มีผู้แนะนำ' && $sub != 'อื่นๆ') {
				$data['known_from'] = $sub;
			} else {
				$data['known_from'] = $subOter;
			}
			$this->StudentModel->updateStudentBooking3($data);
			
			
			// send mail;
		$ttt = $this->StudentModel->getByID($id);
		$this->load->library('email');
		$config['charset'] = 'utf-8';
		$config['wordwrap'] = TRUE;
		$config['mailtype'] = 'html';
		$this->email->initialize($config);		
		$this->email->from('web_admin@tutor-land.com');
		$this->email->to('tutorland@hotmail.co.th');
		$this->email->subject('[C] มีนักเรียนมาสมัครเรียนเพิ่ม  คุณ ' . $ttt['first_name']);
		
		$msgMail = '
		<html>
		<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		</head>
		
		<body>
		<p>มีนักเรียนมาสมัครเรียนเพิ่ม  คุณ' . $ttt['first_name'] . ' ' .$ttt['last_name']. '</p>
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
		    <td>โทรศัพท์</td>
		    <td>'. $ttt['mobile01'] .'</td>
		  </tr>
		  <tr>
		    <td>วันที่ต้องการเริ่มเรียน</td>
		    <td>'. $ttt['study_start_date'] .'</td>
		  </tr>
		</table>
		<br />
		<hr />
		<div align="right"><em>ข้อความนี้ถูกส่งโดยระบบหลังบ้านของติวเตอร์แลนด์<br /> 
		' . date('jS F Y h:i:s A') .'</em></div>
		</body></html>';
		$this->email->message($msgMail);
		$this->email->send();
			
			$data= array();
			$data['success'] = "ได้รับข้อมูลเรียบร้อยแล้วค่ะ ทางโรงเรียนจะติดต่อกลับไปเร็วที่สุดค่ะ";
			$msg['form_status'] = 'add';
			
			
			$this->load->model('filemanagementmodel');
			$data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
			$data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
			$data['banner'] = $this->filemanagementmodel->getByPostion('receipt');
			
			$this->load->view('f_header', $data);
			$this->load->view('f_register_study', $msg);
			$this->load->view('f_footer', $data);
			
		} else {
			$this->index();
		}
	}
}

?>